package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhuandan/data_source"
	"zhuandan/models"
)

type FapiaolistRBody struct {
	Data       []models.Fapiao `json:"data"`
	PageNo     int             `json:"pageNo"`
	PageSize   int             `json:"pageSize"`
	TotalCount int64           `json:"totalCount"`
	TotalPage  int64           `json:"totalPage"`
}

func Fapiaolist(ctx *gin.Context) {
	request := FankuilistBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Fapiao
	res := data_source.Db

	if status > 0 {
		res = res.Where("status = ?", status)
	}

	res.Model(&models.Fapiao{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FapiaolistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type FapiaoshBody struct {
	ID     int64  `json:"id" gorm:"id"`
	Status int64  `json:"status" gorm:"status"` // 2同意 3拒绝
	URL    string `json:"url" gorm:"url"`
	Title  string `json:"title" gorm:"title"`
}

func Fapiaosh(ctx *gin.Context) {
	request := FapiaoshBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	status := request.Status
	url := request.URL
	title := request.Title

	fk := models.Fapiao{
		Status: status,
		Title:  title,
		URL:    url,
	}
	data_source.Db.Where("id = ?", id).Updates(&fk)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
